package com.kunkun.auth.domain.service.Impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.kunkun.auth.common.enums.AuthUserStatusEnum;
import com.kunkun.auth.common.enums.IsDeletedFlagEnum;
import com.kunkun.auth.domain.bo.AuthRoleBO;
import com.kunkun.auth.domain.bo.AuthUserBO;
import com.kunkun.auth.domain.convert.AuthRoleBOConverter;
import com.kunkun.auth.domain.convert.AuthUserBOConverter;
import com.kunkun.auth.domain.redis.RedisUtil;
import com.kunkun.auth.domain.service.AuthRoleDomainService;
import com.kunkun.auth.domain.service.AuthUserDomainService;
import com.kunkun.auth.infra.basic.entity.AuthRole;
import com.kunkun.auth.infra.basic.entity.AuthUser;
import com.kunkun.auth.infra.basic.service.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class AuthRoleDomainServiceImpl implements AuthRoleDomainService {

    @Resource
    private AuthUserService authUserService;

    @Resource
    private AuthRoleService authRoleService;

    @Resource
    private AuthUserRoleService authUserRoleService;

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    @Resource
    private AuthPermissionService authPermissionService;


    private String salt = "chicken";

    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";

    private String authRolePrefix = "auth.role";

    private static final String LOGIN_PREFIX = "loginCode";




    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AuthRoleBO authRoleBO) {

        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);

        authRole.setStatus(AuthUserStatusEnum.OPEN.getCode());
        authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        Integer count = authRoleService.insert(authRole);

        return count > 0;
    }

    @Override
    public Boolean update(AuthRoleBO authRoleBO) {

        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);
        authRole.setStatus(AuthUserStatusEnum.OPEN.getCode());
        authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        Integer count = authRoleService.update(authRole);
        //有任何的更新，都要与缓存进行同步的修改要把当前用户的角色和权限都刷到我们的redis里
        return count > 0;
    }

    @Override
    public Boolean delete(AuthRoleBO authRoleBO) {
        AuthRole authRole = new AuthRole();
        authRole.setId(authRoleBO.getId());
        authRole.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        //有任何的更新，都要与缓存进行同步的修改要把当前用户的角色和权限都刷到我们的redis里
        int count = authRoleService.update(authRole);
        return count > 0;
    }

    @Override
    public AuthUserBO getUserInfo(AuthUserBO authUserBO) {
        return null;
    }

    @Override
    public List<AuthUserBO> listUserInfoByIds(List<String> ids) {
        return null;
    }


}
